package com.niuwa.p2p.core.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.niuwa.p2p.core.constant.SmsSignEnum;

/*
 * 
 * @title 手机号码工具类
 *
 * @author tennyqin
 * @version 1.0
 * @created 2015年3月25日
 */
public class MobileUtil {

	private static Pattern mobilePattern = null;

	static {
		String regEx = "^1\\d{10}$";
		mobilePattern = Pattern.compile(regEx);
	}

	/*
	 * 每3000个手机号码作为一批(普通)
	 */
	public static List<String> nomalMobileSpilt(String destMobiles) {
		List<String> mobileStrs = new ArrayList<String>();
		if (!isEmpty(destMobiles)) {
			int icount = 0;
			StringBuffer mobileBuffer = new StringBuffer();
			String[] mobileArray = destMobiles.split(";");
			for (String mobile : mobileArray) {
				// 3000个为一组
				mobileBuffer.append(mobile);
				mobileBuffer.append(";");
				icount++;
				if (icount >= 3000) {
					mobileStrs.add(mobileBuffer.toString().substring(0,
							mobileBuffer.toString().length() - 1));
					mobileBuffer = new StringBuffer();
				}
			}
			if (icount > 0) {
				mobileStrs.add(mobileBuffer.toString().substring(0,
						mobileBuffer.toString().length() - 1));
			}
		}
		return mobileStrs;
	}

	public static boolean isEmpty(String value) {
		int strLen;
		if (value == null || (strLen = value.length()) == 0) {
			return true;
		}
		for (int i = 0; i < strLen; i++) {
			if ((Character.isWhitespace(value.charAt(i)) == false)) {
				return false;
			}
		}
		return true;
	}

	public static Boolean isMobile(String value) {
		if (isEmpty(value)) {
			return false;
		}
		Matcher matcher = mobilePattern.matcher(value);
		return matcher.find();
	}

	// 是否包含制定的签名 /签名入参 /普通or个性化
	public static Boolean isInludeSign(String contents, String sign,
			SmsSignEnum smsSignEnum) {
		if (isEmpty(sign)) {
			sign = "【牛娃金融】";
		}
		if (isEmpty(contents) || isEmpty(sign)) {
			return false;
		}
		if (SmsSignEnum.Nomal.getValue() == smsSignEnum.getValue()) {
			if (contents.indexOf(sign) > -1) {
				return true;
			} else {
				return false;
			}
		} else { // 个性
			String[] contentArrays = contents.split("\\|\\|");
			for (String content : contentArrays) {
				if (content.indexOf(sign) < 0) {
					return false;
				}
			}
		}
		return true;

	}

	/**
	 * 判断是否是个性化短信 业务规则:相同内容不允许,手机号和内容的分隔符必须符合规范
	 * 
	 * @param destMobiles
	 * @param contents
	 * @return
	 */
	public static Boolean isPersonalSms(String destMobiles, String contents) {
		int mobileLenth = 0;
		int contentLenth = 0;
		// 如果内容相同则无法进行个性化短信发送
		Map<String, String> sameContentSms = new HashMap<String, String>();
		if (!isEmpty(contents) && contents.indexOf("||") > -1) {
			String[] contentArrays = contents.split("\\|\\|");
			contentLenth = contentArrays.length;
			for (String content : contentArrays) {
				String contentKey = MD5Util.hash(content);
				if (!sameContentSms.containsKey(contentKey)) {
					sameContentSms.put(contentKey, contentKey);
				} else {
					return false;
				}
			}
		} else {
			return false;
		}
		// 号码和内容的分隔符数量应该保持一致。
		if (!isEmpty(destMobiles) && destMobiles.indexOf("||") > -1) {
			String[] mobileArrays = destMobiles.split("\\|\\|");
			mobileLenth = mobileArrays.length;
		} else {
			return false;
		}
		if (mobileLenth != contentLenth) { // 分隔符数量不一致
			return false;
		}
		return true;
	}

	/*
	 * 每3000个手机号码作为一批(个性)
	 */
	public static List<String> personalMobileSpilt(String destMobiles) {
		List<String> mobileStrs = new ArrayList<String>();
		if (!isEmpty(destMobiles)) {
			int icount = 0;
			StringBuffer mobileBuffer = new StringBuffer();
			String[] mobileArray = destMobiles.split("\\|\\|");
			for (String mobile : mobileArray) {
				// 3000个为一组
				mobileBuffer.append(mobile);
				mobileBuffer.append("||");
				icount++;
				if (icount >= 3000) {
					mobileStrs.add(mobileBuffer.toString().substring(0,
							mobileBuffer.toString().length() - 2));
					mobileBuffer = new StringBuffer();
				}
			}
			if (icount > 0) {
				mobileStrs.add(mobileBuffer.toString().substring(0,
						mobileBuffer.toString().length() - 2));
			}
		}
		return mobileStrs;
	}
}
